package com.his.registration.controller;

import com.his.common.PageBean;
import com.his.common.Result;
import com.his.dto.DataDoctor;
import com.his.dto.Doctor;
import com.his.dto.RegistLevelIdFee;
import com.his.dto.RegisterForm;
import com.his.registration.pojo.Register;
import com.his.registration.service.*;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: workspace-idea
 * @BelongsPackage: com.his.registration.controller.impl
 * @Author: ZhangYing
 * @Date: 2023/7/1 15:06
 * @Description:
 */
@RestController
@CrossOrigin
@RequestMapping("/register")
public class RegisterController {
    @Autowired
    private RegisterService registerService;
    @Autowired
    private RegistLevelService registLevelService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private SettleCategoryService settleCategoryService;
    @Autowired
    private EmployeeService employeeServic;
    @Autowired
    private SchedulingService schedulingService;

    //获取病历号,传入前端
    @RequestMapping("/getCaseNum")
    public Result getCaseNum(){
        int caseNum = registerService.getCaseNum();
        return Result.success(caseNum);
    }
    //插入患者信息
    @RequestMapping("/insert")
    public Result insert(RegisterForm registerForm){
        //拿id换name
        int deptId = departmentService.getIdByName(registerForm.getDeptName());
        int empId = employeeServic.getIdByName(registerForm.getEmployeeName());
        RegistLevelIdFee registLevelIdFee = registLevelService.getIdFeeByName(registerForm.getRegistName());
        //挂号级别id
        int registId=registLevelIdFee.getId();
        //结算类别id
        int settleCategoryId = settleCategoryService.getIdByName(registerForm.getSettleCategoryName());
        Register register=new Register();
        //得到病历号
        String caseNum = String.valueOf(registerService.getCaseNum());//int转String
        register.setCaseNumber(caseNum);
        register.setRealName(registerForm.getRealName());
        register.setGender(registerForm.getGender());
        register.setCardNumber(registerForm.getCardNumber());
        register.setAge(registerForm.getAge());
        register.setHomeAddress(registerForm.getHomeAddress());
        //后端之前传的给了前端，前端储存下来了，所以还是从前端表单中获取相关数据
        //String->LocalDateTime
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        LocalDateTime visitDate = LocalDateTime.parse(registerForm.getVisitDate(), formatter);
        register.setVisitDate(registerForm.getVisitDate());
//        System.out.println("visitDate的数据为："+visitDate);
        System.out.println("*********"+registerForm.getVisitDate());

        register.setNoon(registerForm.getNoon());
        register.setDeptmentId(deptId);
        register.setEmployeeId(empId);
        register.setRegistLevelId(registId);
        register.setSettleCategoryId(settleCategoryId);
        register.setIsBook(registerForm.getIsBook());
        register.setRegistMethod(registerForm.getRegistMethod());

        //获取结算金额
        Result fee=calculateFee(registerForm.getIsBook(),registerForm.getRegistName());
        register.setRegistMoney((Double) fee.getData());
        //已挂号
        register.setVisitState(1);
        int num = registerService.insert(register);
        return Result.success(num+"条挂号信息插入成功");
    }

    //结算金额，根据是否用病历本和号别类型进行计算
    @RequestMapping("/calculateFee")
    public Result calculateFee(String isBook,String registName){
        //根据registLevelId得到对应和挂号金额
        RegistLevelIdFee registLevelIdFee = registLevelService.getIdFeeByName(registName);
        double fee=registLevelIdFee.getRegistFee();
        //判断是否要了病历本
        if("是".equals(isBook)){
            fee=fee+2;
        }
        return Result.success(fee);
    }

    //获取当前时间，针对医生表和排班表联查来得到对应的医生
    @RequestMapping("/getDoctor")
    public Result getDoctor(int registLevelId,int deptId){
        //获得当前时间
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println("***********"+dateTime);
        int year = dateTime.getYear();
        int month = dateTime.getMonthValue();
        int day = dateTime.getDayOfMonth();
        //判断个日期是星期几
        LocalDate date = LocalDate.of(year, month, day);
        DayOfWeek dayOfWeek = date.getDayOfWeek();

        //创建一个DateDoctor对象传给前端
        DataDoctor dataDoctor=new DataDoctor();
        dataDoctor.setVisitDate(dateTime);

        //判断是上午还是下午
        int hour = dateTime.getHour();
        String noon;
        if (hour < 12) {
            noon="上午";
            dataDoctor.setNoon("上午");
        } else {
            noon="下午";
            dataDoctor.setNoon("下午");
        }

        //得到各医生的值班信息
        List<Doctor> doctorList = schedulingService.getScheduling(registLevelId,deptId);
        List<String> doctorName=new ArrayList<>();

        for (Doctor doctor:
             doctorList) {
            String weekRule = doctor.getWeekRule();
            switch (dayOfWeek) {
                case SUNDAY:
                    if("上午".equals(noon)){
                        if("10".equals(weekRule.substring(0,2))||"11".equals(weekRule.substring(0,2))){
                            doctorName.add(doctor.getRealName());
                        }
                    }
                    else if("下午".equals(noon)){
                        if("01".equals(weekRule.substring(0,2))||"11".equals(weekRule.substring(0,2))){
                            doctorName.add(doctor.getRealName());
                        }
                    }
                    break;
                case MONDAY:
                    if("上午".equals(noon)){
                        if("10".equals(weekRule.substring(2,4))||"11".equals(weekRule.substring(2,4))){
                            doctorName.add(doctor.getRealName());
                        }
                    }
                    else if("下午".equals(noon)){
                        if("01".equals(weekRule.substring(2,4))||"11".equals(weekRule.substring(2,4))){
                            doctorName.add(doctor.getRealName());
                        }
                    }
                    break;
                case TUESDAY:
                    if("上午".equals(noon)){
                        if("10".equals(weekRule.substring(4,6))||"11".equals(weekRule.substring(4,6))){
                            doctorName.add(doctor.getRealName());
                        }
                    }
                    else if("下午".equals(noon)){
                        if("01".equals(weekRule.substring(4,6))||"11".equals(weekRule.substring(4,6))){
                            doctorName.add(doctor.getRealName());
                        }
                    }
                    break;
                case WEDNESDAY:
                    if("上午".equals(noon)){
                        if("10".equals(weekRule.substring(6,8))||"11".equals(weekRule.substring(6,8))){
                            doctorName.add(doctor.getRealName());
                        }
                    }
                    else if("下午".equals(noon)){
                        if("01".equals(weekRule.substring(6,8))||"11".equals(weekRule.substring(6,8))){
                            doctorName.add(doctor.getRealName());
                        }
                    }
                    break;
                case THURSDAY:
                    if("上午".equals(noon)){
                        if("10".equals(weekRule.substring(8,10))||"11".equals(weekRule.substring(8,10))){
                            doctorName.add(doctor.getRealName());
                        }
                    }
                    else if("下午".equals(noon)){
                        if("01".equals(weekRule.substring(8,10))||"11".equals(weekRule.substring(8,10))){
                            doctorName.add(doctor.getRealName());
                        }
                    }
                    break;
                case FRIDAY:
                    if("上午".equals(noon)){
                        if("10".equals(weekRule.substring(10,12))||"11".equals(weekRule.substring(10,12))){
                            doctorName.add(doctor.getRealName());
                        }
                    }
                    else if("下午".equals(noon)){
                        if("01".equals(weekRule.substring(10,12))||"11".equals(weekRule.substring(10,12))){
                            doctorName.add(doctor.getRealName());
                        }
                    }
                    break;
                case SATURDAY:
                    if("上午".equals(noon)){
                        if("10".equals(weekRule.substring(12,14))||"11".equals(weekRule.substring(12,14))){
                            doctorName.add(doctor.getRealName());
                        }
                    }
                    else if("下午".equals(noon)){
                        if("01".equals(weekRule.substring(12,14))||"11".equals(weekRule.substring(12,14))){
                            doctorName.add(doctor.getRealName());
                        }
                    }
                    break;
            }

        }
        dataDoctor.setDoctorName(doctorName);
        return Result.success(dataDoctor);
    }

    //多条件模糊查询患者信息，前端选择截取需要信息
    @RequestMapping("/selectForPatients")
    public Result selectForPatients(String caseNumber,String realName,String visitState){
        int visitStateInt=0;
        if(visitState!=null){
            visitStateInt=Integer.parseInt(visitState);
        }
        List<Register> registers = registerService.selectForPatients(caseNumber, realName,visitStateInt);
        return Result.success(registers);
    }

    //根据病历号进行退号、叫号、接诊结束
    @RequestMapping("/updateVisitState")
    public Result updateVisitState(String caseNumber,String visitType){
        int result=0;
        if("退号".equals(visitType)){
            result = registerService.updateVisitState(caseNumber,4);
        }
        else if("叫号".equals(visitType)){
            result = registerService.updateVisitState(caseNumber,2);
        }
        else if("接诊结束".equals(visitType)){
            result = registerService.updateVisitState(caseNumber,3);
        }
        return Result.success(result+"条看诊状态修改成功");
    }

    //根据看诊状态进行显示
    @RequestMapping("/selectByVisitState")
    public Result selectByVisitState(String visitState,
                                     @RequestParam(defaultValue = "1") int page,
                                     @RequestParam(defaultValue = "10") int pageSize){
//        PageBean pageBean= empService.page(page,pageSize,name,gender,begin,end);
//        return Result.success(pageBean);
//        List<Register> registerList=new ArrayList<>();
        PageBean pageBean=new PageBean();
        if("1".equals(visitState)){//已挂号
            pageBean = registerService.selectByVisitState(1, page, pageSize);
        }
        else if("2".equals(visitState)){//已接诊
            pageBean = registerService.selectByVisitState(2,page,pageSize);
        }
        else if("3".equals(visitState)){//看诊结束
            pageBean = registerService.selectByVisitState(3,page,pageSize);
        }
        else if("4".equals(visitState)){//已退号
            pageBean = registerService.selectByVisitState(4,page,pageSize);
        }
//        return Result.success(registerList);
        return Result.success(pageBean);
    }

    //根据id拿到患者信息
    @RequestMapping("/selectById")
    public Result selectById(int id){
        Register register = registerService.selectById(id);
        return Result.success(register);
    }

    //根据病历号得到挂号id
    @RequestMapping("/getRegistId")
    public Result getRegistId(String caseNumber){
        int registId = registerService.getRegistId(caseNumber);
        return Result.success(registId);
    }

    //确定的查询
    @RequestMapping("/getOne")
    public Result getOne(String caseNumber,String realName){
        Register register = registerService.getOne(caseNumber, realName);
        return Result.success(register);
    }
}
